
/****************************************************************************************************************
* Copyright � Mathematica Policy Research, Inc.
* This code cannot be copied, distributed or used without the express written permission
* of Mathematica Policy Research, Inc.
   Program: 00_include_macros.sas 
   Purpose: Commonly used macros
   Programmer: Christina Sintek/Mike Rudacille/Shinu Verghese
   Project Name: ASPE 51315 - TMSIS COVID
   Date Created: 12/2021
   
****************************************************************************************************************/


/*print masked counts/percents for QA excel download*/
%macro print_masked_counts(dsn = );
	data &dsn.;
	set &dsn.;
	if count >0 and count < 11 then percent = 999;
	run;

	proc print data=&dsn.;
	format count lowc. percent pctmask.;
	run;
%mend print_masked_counts;
%macro freq_tab(indata=, wherecond =, tabvars=,format_stmt=, toxls=1);

proc freq data= &indata. noprint;
table &tabvars./ list missing out=qa_&n.;
&wherecond.;
&format_stmt.;
run;
%if &toxls.=0 %then %do;
  %let n=1;
%end;

%if &toxls.=1 %then %do;
   ods excel select all;
   ods excel options(sheet_interval='now' sheet_name ="qa_&n.");
%end;
%print_masked_counts(dsn = qa_&n.)

%if &toxls.=1 %then %do;
   ods excel select none;
   %let n=%eval(&n. + 1);
%end;

%mend;
%macro recode_stcd_msis;

length submtg_state_cd $2. msis_id $34.;

 if submtg_state_cd_orig in ('94','30') then submtg_state_cd='30';
 else if submtg_state_cd_orig in ('97','42') then submtg_state_cd='42';
 else if submtg_state_cd_orig in ('93','56') then submtg_state_cd='56';
 else submtg_state_cd=submtg_state_cd_orig;

 if submtg_state_cd_orig in ('30','42','56',
                             '94','97', '93') then msis_id=cats(msis_id_orig,submtg_state_cd_orig);
 else msis_id=msis_id_orig;
%mend recode_stcd_msis;

%macro clm_hdr_clean_up;

/**the original submtg_state_cd and msis_id should be renamed before this step**/
  %recode_stcd_msis;

/**flag IL claims to drop and drop them **/
if state_cd= 'IL' and not(adjust_cd = 0 and missing(clm_num_adj)=1) then drop_IL_clm=1;
else drop_IL_clm=0;

%if &fltype. = ip %then %do;

	array dgns  {13} admtg_dgns_cd dgns_cd_1 - dgns_cd_12;

	/**Tennessee is reporting invalid diagnosis codes on the IP claims**/
	do i = 1 to dim(dgns);
	   if state_cd= 'TN'  then dgns{i} = substr(dgns{i}, verify(dgns{i},"0"));
	end;drop i;

 %end;
 

%mend clm_hdr_clean_up;

/**Roll up line dates**/
%macro lne_dts_roll_up_sql;
%if &fltype. = ip or &fltype. = %str(lt) or &fltype. = ot %then %do;
	,min(line_srvc_bgn_dt) as min_line_srvc_bgn_dt format=date9.
	,max(line_srvc_bgn_dt) as max_line_srvc_bgn_dt format=date9.
	,min(line_srvc_end_dt) as min_line_srvc_end_dt format=date9.
	,max(line_srvc_end_dt) as max_line_srvc_end_dt format=date9.
%end;
%mend;

/**Get dates after merging lines dates to header**/
%macro gt_srvc_dts_sql;

%if &fltype. = ip %then %do;
	,coalesce(dschrg_dt, max_line_srvc_end_dt, max_line_srvc_bgn_dt)  as srvc_dt format=date9.

	,case when a.state_cd= 'NE' then min_line_srvc_bgn_dt
		  else coalesce(admsn_dt, min_line_srvc_bgn_dt)
		  end as srvc_bgn_dt_std format=date9.

	,coalesce(dschrg_dt,max_line_srvc_end_dt)  as srvc_end_dt_std format=date9.
%end;
%if &fltype. = %str(lt) %then %do;

 	,coalesce(srvc_end_dt)  as srvc_dt format=date9.
 	,coalesce(srvc_bgn_dt, admsn_dt)  as srvc_bgn_dt_std format=date9.
	,coalesce(srvc_end_dt, dschrg_dt)  as srvc_end_dt_std format=date9.
       
%end;
%if &fltype. = ot %then %do;

	,coalesce(srvc_end_dt) as srvc_dt format=date9.
	,coalesce(srvc_bgn_dt, min_line_srvc_bgn_dt)  as srvc_bgn_dt_std format=date9.
	,coalesce(srvc_end_dt, max_line_srvc_end_dt)  as srvc_end_dt_std format=date9.
%end;
%if &fltype. = rx %then %do; 
	,coalesce(rx_fill_dt)  as srvc_dt format=date9.
%end;
%mend;

%macro recode_stcd_msis_sql;

 ,case when submtg_state_cd_orig in ('94','30') then '30'
      when submtg_state_cd_orig in ('97','42') then '42'
       when submtg_state_cd_orig in ('93','56') then '56'
       else submtg_state_cd_orig end as submtg_state_cd length=2

 ,case when submtg_state_cd_orig in ('30','42','56',
                                     '94','97', '93') 
       then cats(msis_id_orig,submtg_state_cd_orig)
       else msis_id_orig end as msis_id length=34
%mend;

%macro clean_TN_IP_dgns;
%if &fltype. = ip %then %do;

	,case when submtg_state_cd ne '47' then orig_admtg_dgns_cd
		  when submtg_state_cd = '47' and substr(orig_admtg_dgns_cd,1,4) = '0000' then substr(orig_admtg_dgns_cd,5)
		  when submtg_state_cd = '47' and substr(orig_admtg_dgns_cd,1,3) = '000' then substr(orig_admtg_dgns_cd,4)
		  when submtg_state_cd = '47' and substr(orig_admtg_dgns_cd,1,2) = '00' then substr(orig_admtg_dgns_cd,3)
		  when submtg_state_cd = '47' and substr(orig_admtg_dgns_cd,1,1) = '0' then substr(orig_admtg_dgns_cd,2)
		  else orig_admtg_dgns_cd
		  end as admtg_dgns_cd

	%do i = 1 %to 12;
		,case when submtg_state_cd ne '47' then orig_dgns_cd_&i.
			  when submtg_state_cd = '47' and substr(orig_dgns_cd_&i.,1,4) = '0000' then substr(orig_dgns_cd_&i.,5)
			  when submtg_state_cd = '47' and substr(orig_dgns_cd_&i.,1,3) = '000' then substr(orig_dgns_cd_&i.,4)
			  when submtg_state_cd = '47' and substr(orig_dgns_cd_&i.,1,2) = '00' then substr(orig_dgns_cd_&i.,3)
			  when submtg_state_cd = '47' and substr(orig_dgns_cd_&i.,1,1) = '0' then substr(orig_dgns_cd_&i.,2)
		  else orig_dgns_cd_&i.
		  end as dgns_cd_&i.

	%end;

%end;
%mend;

%macro get_ndx_by_filetyp;

%global ndiag;

%if &fltype. = ip %then %do;
   %let ndiag= 12;
%end;
%if &fltype. = %str(lt) %then %do;
   %let ndiag= 5;
%end;
%if &fltype. = ot %then %do;
   %let ndiag= 2;
%end;

%mend;
